
# figured07.fig-- loads modules and performs other editor-related functions
# Copyright (C) 2021 mn
#
# This program is free software: you can redistribute it and/or modify 
#
# it under the terms of the GNU Affero General Public License as published by 
# the Free Software Foundation, either version 3 of the License, or 
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, 
# but WITHOUT ANY WARRANTY; without even the implied warranty of 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License 
# along with this program. If not, see <http://www.gnu.org/licenses/>.

proginf = "fig user-reprogrammable editor 0.7, jul 2021 mn"

buf  "" arr mid 1 0
cmds "" arr mid 1 0
osg  ""
cksv 0
insp 1

params  arr mid 1 0

function vset params k v
python
    params[k] = v
    return params
    fig
    fig

function vget params k
python
    return params[k]
    fig
    fig

function getglcmd gl
    now gl ltrim split now " " mid 1 1 return now
    fig

function gtoken g
    glcmd getglcmd g
    glen g len
    cmdlen glcmd len
    tlen instr g glcmd times -1 minus cmdlen plus glen
    token g right tlen return token
    fig

function rsave p g
python
    save(p, g)
    fig
    fig

function textinsert buf p insp
    blen buf  len
    bef  buf  left insp
    brc  blen minus insp
    aft  arr  mid 1 0
    ifless insp blen
         aft  buf     right brc
         next
    buf  bef  plus p  plus aft
    now  return buf
    fig

python
    params = {}
    pst = []
    if not os.isatty(stdin.fileno()):
        for each in stdin:
            pst += [str(each)[:-1]]
    sys.stdin.close()
    sys.stdin = os.fdopen(1)
    params["pstdin"] = pst
    try:
        osg = (os.getenv("HOME") + "/figured/").replace("//", "/")
        os.chdir(osg)
        from glob import glob
        plugins = glob("*.py")
    except:
        plugins = []
    import readline
    for each in plugins:
        try:
            sig = each.split(".")[0]
            if "." + sig not in cmds and sig != "figured":
                exec("from " + sig + " import " + sig)
                cmds += ["." + sig]
        except:
            pass            
    fig

ck instr cmds ".hello"
iftrue ck
python
    hello(buf, params)
    fig
    next

getargs command join getargs " "
gtrim getargs

while
    iftrue gtrim
        gtrim 0
        g getargs
    else
        try 
           g      lineinput
        except 
           g      "" 
            fig
        fig
ck instr cmds ".gpreproc"
iftrue ck
python
    params["g"] = g
    g = gpreproc(buf, params)
    fig
    next

    gl     g             lcase    ltrim          rtrim
    glen   gl            len
    glcmd  getglcmd gl
    gld    glcmd         mid 1 1  instr gld "."  not

    gt gtoken g
    pb vset params "g" g  vset params "text" buf  vset params "token" gt
    params pb
    pad arr mid 1 0
    pb vset params "pad" pad
    params pb

    ifequal glcmd ".q"
        glcmd ".quit"
        next

    ifequal glcmd ".quit"
        ck instr cmds ".save" times cksv
        iftrue ck
            now rsave buf params
            next
        now end
        next

    ifequal glcmd ".g"
        glcmd ".goto"
        next

    ifequal glcmd ".goto"
        blen  buf len 
        binsp insp
        try
            token gtoken g val int
        except
            insp binsp
            fig
        try
            insp token minus 1
            ifless insp 0
                insp 0
                next
            ifmore insp blen
                insp blen
                next
        except
            insp blen
            next
        next
    pb vset params "insp" insp
    params pb

    ifequal glcmd ".list"
        ck instr cmds ".llist"
        iftrue ck
python
    llist(buf, params)
    fig
            next
        next

    ifequal glcmd ".save"
        cksv 0
        next

    forin e cmds
        el e lcase
        ifequal glcmd el
python
    params["fquit"] = 0
    try: 
        exec("text = " + e[1:] + "(buf, params)")
        if type(text) == dict:
            params = text
            cksv = 1
            pad = params["pad"]
            if pad: buf = textinsert(buf, pad, insp) ; insp += len(pad) ; params["insp"] = insp
            else:   buf = params["text"] ; insp = params["insp"]
    except: pass
    if params["fquit"]: sys.exit()
    fig
            next
        next

    iftrue gld
        blen buf   len
        bef  buf   left insp
        brc  blen  minus insp
        aft  ""    arr         mid 1 0
        ifless insp blen
            aft buf      right brc
            next
        buf  bef   plus g      plus aft
        now  insp  plus 1      swap now insp 
        pb   vset params "insp" insp
        params pb
        cksv 1
        next

    ck instr cmds ".update"
    iftrue ck
python
    update(buf, params)
    fig
        next

    wend


